<?php

/**
 * Subclass for representing a row from the 'receipt_item' table.
 *
 * 
 *
 * @package lib.model
 */ 
class ReceiptItem extends BaseReceiptItem
{
    public function getPriceOfItem()
    {
        return $this->price_of_item;
    }
    public function getTaxForItem()
    {
        return $this->tax_for_item;
    }
    
    public function getPriceFmt()
    {
        return number_format($this->price_of_item, 2);
    }
    public function getPriceByCurrencyCode($currencyCode = 'LTL')
    {
        $arConverter = MyConstants::getCurrencyConverter();
        $currency = $this->getCurrencyCode();
        $toCurrency = $currencyCode;
        $factorKey = $currency . '_' . $toCurrency;
        $price = $this->getPriceOfItem();
        if( !empty($arConverter[$factorKey]) && ($arConverter[$factorKey] != $currency) )
        {
            $price = $this->getPriceOfItem() * $arConverter[$factorKey];
        }
        return $price /100 ;
    }
    
    public function save($con = null)
    {
        //TODO:
        $recalculate = false;
        if( $this->isNew() )
        {
            /* @var $receipt Receipt */
            $recalculate = true;
/*          $receiptId = $this->getReceiptId();
            $c = new Criteria();
            $c->add(ReceiptPeer::ID, $receiptId);
            $receipt = ReceiptPeer::doSelectOne($c);
            if (!empty($receipt))
            {
                $receipt->setReceiptItemsQnt($receipt->getReceiptItemsQnt() + 1);
                $receipt->setTotalPrice($receipt->getTotalPrice() + ($this->getPriceOfItem()*$this->getQuantity()) );
                $receipt->save();
            }*/
        }else 
        {
            if ($this->isColumnModified(ReceiptItemPeer::QUANTITY) || $this->isColumnModified(ReceiptItemPeer::PRICE_OF_ITEM))
            {
                //exit('olditemexit..'. $this->getReceiptId());
                $recalculate = true;
            }
        }
        parent::save($con);
        if ( $recalculate )
        {
            Receipt::recalculate($this->getReceiptId());
        }
        //$this->modifiedColumns[ReceiptItemPeer::QUANTITY];
    }
    public function delete($con = null)
    {
        /*$id = $this->getReceiptId();
        $c = new Criteria();
        $c->add(ReceiptPeer::ID, $id);
        $receipt = ReceiptPeer::doSelectOne($c);
        if (!empty($receipt))
        {
            $receipt->setReceiptItemsQnt($receipt->getReceiptItemsQnt() - 1);
            $receipt->setTotalPrice($receipt->getTotalPrice() - ($this->getPriceOfItem()*$this->getQuantity()) );
            $receipt->save();
        }*/
        parent::delete($con);
        Receipt::recalculate($this->getReceiptId());
    }
}
